package com.loong.tool.secret.base;

import java.io.UnsupportedEncodingException;

import com.loong.tool.secret.exception.SecretVariableException;

/**
 * UTF8处理类
 * 
 * @author 张成轩
 */
public class UTF8 {

	private static final String CHARSET = "UTF-8";

	/**
	 * 获取字节数组
	 * 
	 * @param str 字符串
	 * @return 字节数组
	 */
	public static byte[] getBytes(String str) {

		try {
			return str.getBytes(CHARSET);
		} catch (UnsupportedEncodingException e) {
			throw new SecretVariableException("UTF8 variable init error: CHARSET");
		}
	}

	/**
	 * 获取字符串
	 * 
	 * @param bytes 字节数组
	 * @return 字符串
	 */
	public static String newString(byte[] bytes) {

		try {
			return new String(bytes, CHARSET);
		} catch (UnsupportedEncodingException e) {
			throw new SecretVariableException("UTF8 variable init error: CHARSET");
		}
	}
}
